//
//  HotInformationViewController.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/7/18.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "HotInformationViewController.h"
//#import "DiscoverViewModel.h"
//#import "DiscoverModel.h"
//#import "DiscoverCell.h"
#import "HotInformationViewModel.h"
#import "HotInformationModel.h"
#import "HotInformationCell.h"
#import "NewsDetailsViewController.h"
@interface HotInformationViewController ()<
UITableViewDelegate,
UITableViewDataSource
>
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) HotInformationViewModel * hotViewModel;
@end

@implementation HotInformationViewController

-(HotInformationViewModel *) hotViewModel{
    if (!_hotViewModel) {
        _hotViewModel = [[HotInformationViewModel alloc] init];
    }
    return _hotViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
    [self initUI];
    [self setupRefresh];
    
    
}
-(void)setupRefresh{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(mj_headerRefresh)];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(mj_footerRefresh)];
    
}

-(void)mj_headerRefresh{
    
    self.hotViewModel.page=0;
    [self initData];
    
}

-(void)mj_footerRefresh{
    
    [self initData];
    
}

-(void)initData{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.hotViewModel noticeListResultRequestParameter:LoadData] subscribeNext:^(id result) {
        
        if([result[@"errorcode"] integerValue] == 0){
            
            [self.tableView reloadData];
            
        }
        
        [MXUtility hideMBProgress:self.view];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
}

-(void)initNav{
    
    [self.navigationItem setTitle:@"热门资讯"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
}

-(void)initUI{
    //    5条新闻
    self.tableView=[UITableView new];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH);
        make.height.mas_equalTo(DEF_CONTENT_INTABBAR_HEIGHT);
        make.top.equalTo(self.view).with.offset(0);
        make.leading.equalTo(self.view).with.offset(0);
    }];
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.hotViewModel.noticeArrayList.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"hotCell";
    HotInformationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell = [[HotInformationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.model=self.hotViewModel.noticeArrayList[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HotInformationModel* model=self.hotViewModel.noticeArrayList[indexPath.row];
    //    新闻详情页
    NewsDetailsViewController* vc=[[NewsDetailsViewController alloc]init];
    vc.hidesBottomBarWhenPushed=YES;
    vc.newsId=model.noticenewsId;
    vc.controllerFromWhere=UIViewControllerFromOther;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 1.0);
    cell.alpha = 0.0;
    cell.layer.transform = transform3D;
    cell.layer.anchorPoint = CGPointMake(0.0, 0.5); // 设置锚点位置；默认为中心点(0.5, 0.5)
    
    [UIView beginAnimations:@"transform" context:NULL];
    [UIView setAnimationDuration:0.5];
    cell.alpha = 1.0;
    cell.layer.transform = CATransform3DIdentity;
    CGRect rect = cell.frame;
    rect.origin.x = 0.0;
    cell.frame = rect;
    [UIView commitAnimations];
}



@end
